﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NearForums.Configuration;
using NearForums.Configuration.Settings;
using NearForums.Configuration.Spam;

namespace NearForums.DataAccess.Xml
{
    public class XmlSettingsRepository : BaseDataAccess, ISettingsRepository
    {
        public Configuration.SiteConfiguration LoadSettings(Configuration.SiteConfiguration config)
        {
            LoadElement(config.General, "general");
            LoadElement(config.UI, "ui");
            LoadElement(config.SpamPrevention, "spamPrevention");
            LoadElement(config.Search, "search");
            return config;
        }

        public void SaveSetting(Configuration.SettingConfigurationElement element)
        {
            string elementName = null;
			if (element is GeneralElement)
			{
				elementName = "general";
			}
			else if (element is UIElement)
			{
				elementName = "ui";
			}
			else if (element is SpamPreventionElement)
			{
				elementName = "spamPrevention";
			}
			else if (element is SearchElement)
			{
				elementName = "search";
			}
			else
			{
				throw new NotSupportedException("Element of type " + element.GetType().Name + " is not supported.");
			}
			element.ValidateFields();
			SaveElement(element, elementName);
        }

        private void SaveElement(SettingConfigurationElement element, string elementName)
        {
            var builder = new StringBuilder();
            element.Serialize(builder, elementName);

            string filePath = GetDataPath(SettingsFile);
             XDocument doc = XDocument.Load(filePath);
            var qry = from s in doc.Root.Elements("Setting")
                      where (string)s.Attribute("Name") == elementName
                      select s;
            var elem = qry.SingleOrDefault();
            if (elem != null)
            {
                elem.Value = builder.ToString();
            }
            else 
            {
                elem = new XElement("Setting",
                            new XAttribute("Name", elementName),
                            builder.ToString());
                doc.Root.Add(elem);
            }

            doc.Save(filePath);

        }

        private void LoadElement(SettingConfigurationElement element, string elementName)
        {
            XDocument doc = XDocument.Load(GetDataPath(SettingsFile));
            var qry = from s in doc.Root.Elements("Setting")
                      where (string)s.Attribute("Name") == elementName
                      select s;
            var elem = qry.SingleOrDefault();
            if (elem != null)
            {
                var settingsXml = elem.Value;
                if (settingsXml != null && settingsXml.Length > 0)
                {
                    element.Deserialize(new StringBuilder(settingsXml));
                }
            }
        }
    }
}
